package com.bjpowernode.boss.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    public static final ObjectMapper MAPPER = new ObjectMapper();
    public static String beanToJson(Object data) {
        // 将对象转为json的方法
        try {
            return MAPPER.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T jsonToBean(String userJson, Class<T> clazz) {
        try {
            return MAPPER.readValue(userJson, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
